from typing import Dict, List, Tuple


def flatten_json(data: Dict, parent_key: str = '') -> List[Tuple[str, str]]:
    """
    扁平化 JSON，提取 (field, value) 元组

    Args:
        data: JSON 数据
        parent_key: 父键名

    Returns:
        List of (field, value) tuples
    """
    items = []
    if isinstance(data, dict):  # 检测根为 dict 类型
        for k, v in data.items():
            new_key = f"{parent_key}.{k}" if parent_key else k
            if isinstance(v, dict):
                items.extend(flatten_json(v, new_key))
            elif isinstance(v, bool):
                continue
            elif isinstance(v, (str, int, float)):
                items.append((new_key, str(v)))
            elif isinstance(v, list):
                # 快速解析长度为 1 的对象数组
                if len(v) == 1:
                    items.append((new_key, str(v[0])))
                    continue
                # 递归解析对象数组
                for i, item in enumerate(v):
                    new_key = f"{new_key}.{i}"
                    if isinstance(item, dict):
                        items.extend(flatten_json(item, new_key))
                    elif isinstance(item, (str, int, float)) and not isinstance(item, bool):
                        items.append((new_key, str(item)))
                    else:
                        raise Exception(f"unsupported json type: {type(item)}")
            else:
                raise Exception(f"unsupported json type: {type(v)}")
    return items
